package org.summer.lawyer.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.lawyer.domain.vo.article.ArticleCreateVo;
import org.summer.lawyer.domain.vo.article.ArticleUpdateVo;
import org.summer.lawyer.domain.vo.article.ArticleVo;
import org.summer.lawyer.service.IArticleService;

import java.util.List;

/**
 *
 */
@Tag(name = "后台相关接口", description = "文章管理")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/article")
public class ArticleController {

    private final IArticleService articleService;

    @Operation(description = "文章列表查询")
    @SaCheckPermission("article:article:list")
    @GetMapping("/list")
    public TableDataInfo<ArticleVo> list(ArticleVo vo, PageQuery pageQuery){
        return articleService.selectListPage(vo, pageQuery);
    }

    @Operation(description = "新增文章")
    @Log(title = "新增文章", businessType = BusinessType.INSERT)
    @SaCheckPermission("article:article:add")
    @PostMapping("/saveArticle")
    public R<ArticleVo> saveArticle(@Validated @RequestBody ArticleCreateVo vo){
        articleService.saveArticle(vo);
        return R.ok();
    }

    @Operation(description = "修改文章")
    @Log(title = "修改文章", businessType = BusinessType.UPDATE)
    @SaCheckPermission("article:article:edit")
    @PostMapping("/updateArticle")
    public R<ArticleVo> updateArticle(@Validated @RequestBody ArticleUpdateVo vo){
        articleService.updateArticle(vo);
        return R.ok();
    }

    @Operation(description = "删除文章")
    @Log(title = "删除文章", businessType = BusinessType.DELETE)
    @SaCheckPermission("article:article:remove")
    @DeleteMapping("/{ids}")
    public R<Void> removeArticle(@NotEmpty(message = "主键不能为空")@PathVariable("ids")Long[] ids){
        articleService.deleteArticleByIds(List.of(ids));
        return R.ok();
    }

    @Operation(description = "获取文章信息")
    @SaCheckPermission("article:article:query")
    @GetMapping("/{id}")
    public R<ArticleVo> getInfo(@NotNull(message = "主键不能为空")@PathVariable("id")Long id){
        return R.ok(articleService.selectArticleVoById(id));
    }
}
